import * as courseDetailService from '../../services/courseDetail'
import {setcookie} from '../../utils/cookies'
import {getcookie} from '../../utils/cookies'
import {routerRedux} from 'dva/router'
import {EXPIRE_SECONDS} from '../../utils/constants'
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}



export default {
  namespace: 'courseDetail',
  state: {courseBri:{},getBriState:null,courseBriInfo:{teacherBriList:[{imgUrl:'',realName:'',teacherIntroduction:''}],
    course:{courseImgName:'',courseDetailImgUrl:'',consultTeacher:{phone:1,wechatNumber:1,imgUrl:''}},classList:[]},
    curLiveList:[],
    chapterList:[
      {
        "open":true,
        "sectionId": 1,
        "sectionPri": 1,
        "sectionDefaultName":1,
        "sectionTitle": "我是第一节的标题",
        "sectionTask": [
          {
            "taskPri": 1,
            "taskId": 1,

            "taskType": "录播",
            "taskTitle": "我是任务的标题",
            "taskStudyStartTime": "2017年5月21日 20:00",
            "taskStudyEndTime": "22:00",
            "videoUrl": "我是视频地址",
            "videoDuration": "120分钟",
            "linkTeacher": "我是关联老师",
            "linkClass": "我是关联班级",
            "taskState":'未查看'
          }
        ],


      },

    ]
  },
  reducers: {
    getcourseBriSave(state,{payload}){
      if (payload.courseBri.course.consultTeacher == undefined) {
        payload.courseBri.course.consultTeacher = {phone:1,wechatNumber:1,imgUrl:''}
      }
      if (payload.courseBri.teacherBriList == undefined) {
        payload.courseBri.teacherBriList = []
      }




      return {
        ...state,courseBriInfo:payload.courseBri,
        getBriState:true,

      }
    },
    chapterListSave(state,{payload}){
      // console.log("------payload",payload)

      //找出当前的直播
      let curLiveList = []
     payload.chapterList.map((item)=>{
        if (item.sectionTask.length > 0) {
          item.sectionTask.map((i)=>{
            if (i.taskType == 'LIVE' && i.taskStateAdd.taskSeeState == '进行中') {
              curLiveList.push(item)
            }
          })
        }
      })
      // console.log("----curLiveList--",curLiveList)

      return {
        ...state,chapterList:payload.chapterList,curLiveList:curLiveList,
        getChapterState:true
      }
    },
    resetState(state,{payload}){
      return {
        ...state,getBriState:payload.getBriState,
        getChapterState:payload.getBriState,

      }
    }


  },
  effects: {
    *getcourseBri({payload:values},{call,put}){

      const {data} = yield call (courseDetailService.getcourseBrinfo,values)
      // console.log("----------data",data)
      // yield put({type:"getCourseIndexSave",payload:{data}})
      if (data.success == true) {
        let courseBri = data.data
        //算出班级的最高价格和最低价格
        if (courseBri.classList != undefined) {

          if (courseBri.classList.length > 1) {
            let heighterPrice = courseBri.classList[0].actualCnyPrice
            let lowerPrice = courseBri.classList[0].actualCnyPrice
            let originLowerPrice = courseBri.classList[0].originCnyPrice
            let originHeightPrice = courseBri.classList[0].originCnyPrice

            let heighterYcoin = courseBri.classList[0].actualYcoinPrice
            let lowerYcoin = courseBri.classList[0].actualYcoinPrice
            let originHeighterYcoin = courseBri.classList[0].originYcoinPrice
            let originLowerYcoin = courseBri.classList[0].originYcoinPrice

            for (let i = 1; i < courseBri.classList.length; i++) {

              if (heighterPrice < courseBri.classList[i].actualCnyPrice) {
                heighterPrice = courseBri.classList[i].actualCnyPrice

              }
              if (lowerPrice > courseBri.classList[i].actualCnyPrice) {
                lowerPrice = courseBri.classList[i].actualCnyPrice

              }
              if (heighterYcoin < courseBri.classList[i].actualYcoinPrice) {
                heighterYcoin = courseBri.classList[i].actualYcoinPrice
              }
              if (lowerYcoin > courseBri.classList[i].actualYcoinPrice) {
                lowerYcoin = courseBri.classList[i].actualYcoinPrice
              }
              if (originHeightPrice < courseBri.classList[i].originCnyPrice) {
                originHeightPrice = courseBri.classList[i].originCnyPrice
              }
              if (originLowerPrice > courseBri.classList[i].originCnyPrice) {
                originLowerPrice = courseBri.classList[i].originCnyPrice
              }
              if (originHeighterYcoin < courseBri.classList[i].originYcoinPrice) {
                originHeighterYcoin = courseBri.classList[i].originYcoinPrice
              }
              if (originLowerYcoin > courseBri.classList[i].originYcoinPrice) {
                originLowerYcoin = courseBri.classList[i].originYcoinPrice
              }



              courseBri.heighterPrice = heighterPrice/100
              courseBri.lowerPrice = lowerPrice/100

              courseBri.heighterYcoin = heighterYcoin
              courseBri.lowerYcoin = lowerYcoin

              courseBri.originLowerPrice = originLowerPrice/100
              courseBri.originHeightPrice = originHeightPrice/100

              courseBri.originHeighterYcoin = originHeighterYcoin
              courseBri.originLowerYcoin = originLowerYcoin

            }

          } else if (courseBri.classList.length = 1) {
            courseBri.lowerPrice = courseBri.classList[0].actualCnyPrice/100
            courseBri.heighterPrice = courseBri.classList[0].actualCnyPrice/100
            courseBri.originLowerPrice = courseBri.classList[0].originCnyPrice/100
            courseBri.originHeightPrice = courseBri.classList[0].originCnyPrice/100

            courseBri.heighterYcoin = courseBri.classList[0].actualYcoinPrice
            courseBri.lowerYcoin = courseBri.classList[0].actualYcoinPrice
            courseBri.originLowerYcoin = courseBri.classList[0].originYcoinPrice
            courseBri.originHeighterYcoin = courseBri.classList[0].originYcoinPrice

          } else {
            courseBri.lowerPrice = 0
            courseBri.heighterPrice = 0
            courseBri.originLowerPrice = 0
            courseBri.originHeightPrice = 0

            courseBri.heighterYcoin = 0
            courseBri.lowerYcoin = 0
            courseBri.originLowerYcoin = 0
            courseBri.originHeighterYcoin = 0
          }


        }



        courseBri.course.courseBeginTime = new Date(courseBri.course.courseBeginTime).format("yyyy-MM-dd hh:mm:ss")

        // courseBri = courseBri


        yield put({type:"getcourseBriSave",payload:{courseBri,values}})
      }












      let courseBri = {
        "coursePri":1,
        "courseBri":"我是课程简介",
        "orgId":"云逸",
        "courseImgName": "carousel-20170808225507.png",
        "weight":1,
        "courseId": 1001,
        "courseName": "数学一 张宇数学 强化班 ",
        "courseGroup":"轮播图",
        "isFree": true,
        "commonCash": 100,
        "commonYcoin": 10000,
        "discountCash": 0,
        "discountYcoin": 0,
        "enrollmentBeginTime": 416964391641,
        "enrollmentEndTime": 416964391641,
        "courseBeginTime":416964391641,
        "courseEndTime":416964391641,
        "salesNum":100,

        "teacherBriList":[
          {
            "teacherId":1,
            "teacherName":"朱伟",
            "teacherAvatar":"courseClass-20170807175006.jpg",
            "teacherBriText":"我是老师简介介绍文字我是老师简介介绍文字我是老师简介介绍文字" +
            "我是老师简介介绍文字我是老师简介介绍文字我是老师简介介绍文字我是老师简介介绍文字我是" +
            "老师简介介绍文字我是老师简介介绍文字",
          },
          {
            "teacherId":2,
            "teacherName":"朱伟",
            "teacherAvatar":"courseClass-20170807175006.jpg",
            "teacherBriText":"我是老师简介介绍文字",
          },
          {
            "teacherId":3,
            "teacherName":"朱伟",
            "teacherAvatar":"courseClass-20170807175006.jpg",
            "teacherBriText":"我是老师简介介绍文字",
          },
          {
            "teacherId":4,
            "teacherName":"朱伟",
            "teacherAvatar":"courseClass-20170807175006.jpg",
            "teacherBriText":"我是老师简介介绍文字",
          }
        ],
        "courseDetailImg":"courseClass-20170807175006.jpg",
        "classList":[
          {
            "classId":4,
            "className":"我是班级一的名字",
            "classBri":"我是班级一的介绍",
            "classPrise":1222
          },
          {
            "classId":5,
            "className":"我是班级一的名字",
            "classBri":"我是班级一的介绍",
            "classPrise":1222
          },
          {
            "classId":6,
            "className":"我是班级一的名字",
            "classBri":"我是班级一的介绍",
            "classPrise":1222
          }
        ]


      }
       let courseCoverInfo = {
        // courseTitle:
       }









      // yield put({type:"getcourseBriSave",payload:{courseBri}})


        },
    *getChapterInfo({payload:values},{call,put}){
      const {data} = yield call (courseDetailService.getChapterInfo,values)
      // console.log("-----------data",data)
      if (data.success == true) {


          if (data.data.length > 0) {
            data.data.map((item)=>{
              item.open = true
              if (item.sectionTask.length > 0) {
                item.sectionTask.map((i)=>{
                  //直播
                  if (i.taskType == "LIVE") {
                    if (i.liveBroadcastTimeEnd < new Date().getTime()) {
                      let taskSeeState = "已结束"
                      i.taskStateAdd = {
                        taskDurationTime: parseInt((i.liveBroadcastTimeEnd - i.liveBroadcastTimeFrom) / (60 * 1000)),
                        taskSeeState: taskSeeState,
                        taskStudyStartTime: new Date(i.liveBroadcastTimeFrom).format("hh:mm"),
                        taskStudyEndTime: new Date(i.liveBroadcastTimeEnd).format("hh:mm"),
                        taskStudyDate: new Date(i.liveBroadcastTimeFrom).format("MM月dd日")
                      }
                    } else if (i.liveBroadcastTimeFrom > new Date().getTime()) {
                      let taskSeeState = "未开始"
                      i.taskStateAdd = {
                        taskDurationTime: parseInt((i.liveBroadcastTimeEnd - i.liveBroadcastTimeFrom) / (60 * 1000)),
                        taskSeeState: taskSeeState,
                        taskStudyStartTime: new Date(i.liveBroadcastTimeFrom).format("hh:mm"),
                        taskStudyEndTime: new Date(i.liveBroadcastTimeEnd).format("hh:mm"),
                        taskStudyDate: new Date(i.liveBroadcastTimeFrom).format("MM月dd日")
                      }
                    } else if (i.liveBroadcastTimeFrom <= new Date().getTime() && new Date().getTime() <= i.liveBroadcastTimeEnd) {
                      let taskSeeState = "进行中"
                      i.taskStateAdd = {
                        taskDurationTime: parseInt((i.liveBroadcastTimeEnd - i.liveBroadcastTimeFrom) / (60 * 1000)),
                        taskSeeState: taskSeeState,
                        taskStudyStartTime: new Date(i.liveBroadcastTimeFrom).format("hh:mm"),
                        taskStudyEndTime: new Date(i.liveBroadcastTimeEnd).format("hh:mm"),
                        taskStudyDate: new Date(i.liveBroadcastTimeFrom).format("MM月dd日")
                      }

                    }


                  }
                  //回放
                  if (i.taskType == "PLAYBACK") {
                    i.taskStateAdd = {
                      taskDurationTime: parseInt((i.suggestPlaybackTimeEnd - i.suggestPlaybackTimeFrom) / (60 * 1000)),
                      taskSeeState: '回放未观看',
                      taskStudyStartTime: new Date(i.suggestPlaybackTimeFrom).format("hh:mm"),
                      taskStudyEndTime: new Date(i.suggestPlaybackTimeEnd).format("hh:mm"),
                      taskStudyDate: new Date(i.suggestPlaybackTimeFrom).format("MM月dd日")
                    }

                  }
                  //录播
                  if (i.taskType == "VIDEO") {
                    i.taskStateAdd = {
                      taskDurationTime: parseInt((i.suggestPlaybackTimeEnd - i.suggestPlaybackTimeFrom) / (60 * 1000)),
                      taskSeeState: '已上传',
                      taskStudyStartTime: new Date(i.suggestPlaybackTimeFrom).format("hh:mm"),
                      taskStudyEndTime: new Date(i.suggestPlaybackTimeEnd).format("hh:mm"),
                      taskStudyDate: new Date(i.suggestPlaybackTimeFrom).format("MM月dd日")
                    }

                  }
                  //作业
                  if (i.taskType == "HOMEWORK") {
                    if (i.materialSize/1024/1024 > 1) {
                      i.materialSize = parseInt(i.materialSize / 1024 / 1024) +'M'
                    } else {
                      i.materialSize = parseInt(i.materialSize / 1024) + 'K'
                    }
                    i.taskStateAdd = {
                      taskDurationTime: parseInt((i.homeworkSubmitTimeEnd - i.homeworkSubmitTimeFrom) / (60 * 1000)),
                      taskSeeState: '未查看',
                      taskStudyStartTime: new Date(i.homeworkSubmitTimeFrom).format("hh:mm"),
                      taskStudyEndTime: new Date(i.homeworkSubmitTimeEnd).format("hh:mm"),
                      taskStudyDate: new Date(i.homeworkSubmitTimeFrom).format("MM月dd日 hh时mm分")
                    }

                  }
                  //资料
                  if (i.taskType == "MATERIAL") {
                    if (i.materialSize / 1024 / 1024 > 1) {
                      i.materialSize = parseInt(i.materialSize / 1024 / 1024) + 'M'
                    } else {
                      i.materialSize = parseInt(i.materialSize / 1024) + 'K'
                    }
                    i.taskStateAdd = {
                      taskDurationTime: parseInt((i.homeworkSubmitTimeEnd - i.homeworkSubmitTimeFrom) / (60 * 1000)),
                      taskSeeState: '未查看',
                      taskStudyStartTime: new Date(i.homeworkSubmitTimeFrom).format("hh:mm"),
                      taskStudyEndTime: new Date(i.homeworkSubmitTimeEnd).format("hh:mm"),
                      taskStudyDate: new Date(i.homeworkSubmitTimeFrom).format("MM月dd日")
                    }

                  }

                  return i

                })
              }
              // return item
            })

          }
          // that.setData({ chapterList: that.data.chapterList})
          // console.log("-----that.data.chapterList", that.data.chapterList)




        // console.log("--------------data.data",data.data)
        let chapterList = data.data

        yield put({type:"chapterListSave",payload:{data,chapterList,values}})
      }

    },
    *goToLive({payload:values},{call,put}){
      // console.log("----------values",values)
      if (values.liveVideoUrl != undefined) {

        let liveVideoUrl = values.liveVideoUrl.replace("m3u8","flv")

        setcookie("liveVideoUrl", liveVideoUrl,EXPIRE_SECONDS);
        yield put(routerRedux.replace({pathname:'/playLive',}))

      }
    },
    *goBack({payload:values},{call,put}){
      if (values.item.id == 1) {
        yield put(routerRedux.replace({pathname:'/classIndex',}))
      }
    }




  },
  subscriptions: {},
};
